#include "Loading/OffWriter.hpp"

namespace mif
{
	
	OffWriter::OffWriter()
	{
		//EMPTY
	}
	
	OffWriter::~OffWriter()
	{
		//EMPTY
	}
	
	int OffWriter::Write(std::string pFileName, Mesh & pMesh)
	{
		
		std::cout << std::endl << "Writing in : " << pFileName << std::endl;
		
		/*
			Creation du fichier
		*/
		std::ofstream vOutfile;
		vOutfile.open(pFileName);
		
		/*
			Ajout du type OFF
		*/
		vOutfile<<"OFF"<<std::endl;
		
		/*
			Ajout de l'en-tete -> NbVertex  NbFace NbEdge
		*/
		vOutfile << pMesh.GetNbVertex() << " " << pMesh.GetNbFace()<< " "  << pMesh.GetNbEdge() << std::endl;
		
		/*
			Ajout des coordonnées de point
		*/
		
		std::cout << "\tWriting Vertex ..." << std::endl;
		
		for(auto vVertexIt = pMesh.VertexBegin(); vVertexIt != pMesh.VertexEnd(); vVertexIt++)
			vOutfile << (*vVertexIt).GetCoord().x << " " << (*vVertexIt).GetCoord().y << " " << (*vVertexIt).GetCoord().z << std::endl;
		
		/*
			Ajout des faces : ID, v1, v2, v3, r, g, b, a
		*/
		
		std::cout << "\tWriting Face ..." << std::endl;
		
		for(auto vFaceIt = pMesh.FaceBegin(); vFaceIt != pMesh.FaceEnd(); vFaceIt++)
			vOutfile << "3 " << (*vFaceIt).GetTriangle().a->GetID() << " " 
						     << (*vFaceIt).GetTriangle().b->GetID() << " " 
						     << (*vFaceIt).GetTriangle().c->GetID() << " "
						     << (*vFaceIt).GetColor().r << " "
						     << (*vFaceIt).GetColor().g << " "
						     << (*vFaceIt).GetColor().b << " " 
						     << (*vFaceIt).GetColor().a << std::endl;
												  
		/*
			Fermeture du fichier
		*/
		vOutfile.close();
	
		std::cout<<"End of writing" << std::endl << std::endl;
	
		return 0;
	}
	
}
